package com.ocp.macro.controller;

import com.alibaba.fastjson.JSONObject;
import com.ocp.macro.entity.AlctProduct;
import com.ocp.macro.entity.Allocation;
import com.ocp.macro.entity.DealerAccount;
import com.ocp.macro.service.DealerAccountService;
import com.ocp.macro.service.DealerListService;
import com.ocp.macro.utils.GsonUtils;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@CrossOrigin
@RestController
@Secured("ROLE_dealAccount")
public class DealerAccountController {
    /**
     * 业务处理层依赖注入
     */
    @Resource
    private DealerAccountService dealerAccountService;

    /**
     *  实现：孙历
     * 请求该总帐号下的所有子账号，根据dealerAccount封装的信息筛选
     * 封装的信息有：用户名称(userName)，账号状态信息(status)，真实姓名(realName)，手机号(telephone)
     * @param dealerAccount 筛选信息
     */
    @PostMapping("/macro/dealer/subaccountlist")
    public String dealerSubAccountList(DealerAccount dealerAccount){
        return dealerAccountService.selectDealerSubAccountList(dealerAccount,null);
    }

    /**
     * 实现：孙历
     * 请求新增经销商子账号
     * @param dealerAccount 子账号信息
     */
    @PostMapping("/macro/dealer/subacctadd")
    public String dealerSubAccount(DealerAccount dealerAccount){
        return dealerAccountService.addDealerSubAccount(dealerAccount,null);
    }

    /**
     * 实现：孙历
     * 批量请求删除所选的经销商子账号
     * 前端将所选的账号id生成一个数组封装到list
     * @param subaccount 封装账号id数组的对象
     */
    @PostMapping("/macro/dealer/subacctdelete")
    public String dealerSubAccountDelete(Integer[] subaccount){
        return dealerAccountService.dealerSubAccountDelete(Arrays.asList(subaccount));
    }

    /**
     * 实现：孙历
     * 批量请求所选的 经销商子账号 进行 账号状态设置
     * 前端将所选的 账号id 封装成一个数组，赋值给list
     * 和传的status字段的值：0禁用，1启用
     * @param state 封装参数的对象
     */
    @PostMapping("/macro/dealer/subacctstate")
    public String dealerSubAccountState(Integer state,Integer[] subaccount){
        return dealerAccountService.dealerSubAccountState(state, Arrays.asList(subaccount));
    }

    /**
     * 实现：孙历
     * 经销商子账号进行信息修改请求
     * @param dealerAccount 新的子账号信息
     */
    @PostMapping("/macro/dealer/subacctmodify")
    public String dealerSubAccountModify(DealerAccount dealerAccount){
        return dealerAccountService.dealerSubAccountModify(dealerAccount);
    }

    /**
     * 实现：孙历
     * 请求当前登录的账号所关联的经销商信息
     */
    @GetMapping("/macro/dealer/dealeracctdistrs")
    public String dealerAccountDistrList(){
        return dealerAccountService.dealerAccountDistrList(null);
    }

    /**
     * 实现：孙历
     * 请求将所选的 经销商子账号 进行 关联经销商信息
     * @param realNos 所选的经销商信息编号
     * @param subAcctId 要进行关联的子账号ID
     */
    @PostMapping("/macro/dealer/subacctreldistr")
    public String dealerSubAcctRelDistr(@RequestParam("realNos") List<String> realNos,
                                        @RequestParam("subAcctId")Integer subAcctId){
        return  dealerAccountService.addDealerAccountListRel(realNos,subAcctId);
    }

    // 查询经销商名是否存在
    @GetMapping("/macro/dealer/dealnameexists")
    public String dealerAccountDealNameExists(String dealerName){
        return  dealerAccountService.dealerNameIsExists(dealerName);
    }

    /**
     * 实现：孙历
     * 重置子经销商密码为123456
     * @param subAcctId
     * @return
     */
    @PostMapping("/macro/dealer/dealeraccresetPwd")
    public String dealerAccountResetPwd(Integer[] subAcctId){
        return dealerAccountService.dealerAccountResetPwd(subAcctId);
    }

}
